package com.pyw.http_client_test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.language.bm.Rule.RPattern;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pyw.http_client_test.entity.AccessToken;
import com.pyw.http_client_test.entity.Category;
import com.pyw.http_client_test.entity.Product;
import com.pyw.http_client_test.entity.ProductDetailResult;
import com.pyw.http_client_test.entity.ProductResult;
import com.pyw.http_client_test.util.ExcelUtil;
import com.pyw.http_client_test.util.HttpUtil;

/**
 * Hello world!
 *
 */
public class App {

	public static void main(String[] args) {
		AccessToken accessToken = geAccessToken();
		// 获取所有分类
//		List<Category> categories = getCategory(accessToken.getAccessToken());
//		for (Category category : categories) {
//			System.out.println(category.getCategoryFirstName());
//		}

		// 获取商品参数
		HashMap<String, Object> productParams = new HashMap<String, Object>();
		// 页数
		productParams.put("pageNum", 1);

		// 每页返回多少条
//		productParams.put("pageSize", 20);
//		
		ProductResult productList = getProductList(accessToken.getAccessToken(), productParams);
		List<Product> list = productList.getList();

		// 数据写入excel
//	    ExcelUtil.write("D://test.xls",Product.class,"Product",list);

		// 以下可根据需要传
		// productParams.put("categoryId", 种类ID);
		// productParams.put("categoryKeyword", 种类关键字);
		// productParams.put("pid", 商品 id);
		// productParams.put("productSku", 商品SKU);
		// productParams.put("productType", 商品类型);

		// 商品详情

		// 获取商品参数
		HashMap<String, Object> productDeatailParams = new HashMap<String, Object>();
		Product product = list.get(0);
		// pid
		productDeatailParams.put("pid", product.getPid());
		ProductDetailResult productDetail = getProductDetailList(accessToken.getAccessToken(), productDeatailParams);
		System.out.println(productDetail.getDescription());
	}

	/**
	 * 得到认证令牌
	 * 
	 * @return
	 */
	public static AccessToken geAccessToken() {
		Map<String, String> paramsMap = new HashMap<>();
		paramsMap.put("email", "batu1016@gmail.com");
		paramsMap.put("password", "a8e53926398743b4b0cdc627e9d027ad");
		String accessTokenPost = HttpUtil.doPost(
				"https://developers.cjdropshipping.com/api2.0/v1/authentication/getAccessToken",
				JSON.toJSONString(paramsMap));
		JSONObject parseObject = JSONObject.parseObject(accessTokenPost);
		return JSONObject.parseObject(parseObject.get("data").toString(), AccessToken.class);
	}

	/**
	 * 得到分类
	 * 
	 * @param token
	 * @return
	 */
	public static List<Category> getCategory(String token) {
		Header header = new BasicHeader("CJ-Access-Token", token);
		String caString = HttpUtil.doGet("https://developers.cjdropshipping.com/api2.0/v1/product/getCategory", null,
				header);
		JSONObject parseObject = JSONObject.parseObject(caString);
		List<Category> parseArray = JSONArray.parseArray(parseObject.get("data").toString(), Category.class);
		return parseArray;
	}

	/**
	 * 得到商品列表
	 * 
	 * @param token
	 * @return
	 */
	public static ProductResult getProductList(String token, HashMap<String, Object> params) {
		Header header = new BasicHeader("CJ-Access-Token", token);
		String caString = HttpUtil.doGet("https://developers.cjdropshipping.com/api2.0/v1/product/list", params,
				header);
		JSONObject parseObject = JSONObject.parseObject(caString);

		return JSONObject.parseObject(parseObject.get("data").toString(), ProductResult.class);
	}

	/**
	 * 得到商品详情列表
	 * 
	 * @param token
	 * @return
	 */
	public static ProductDetailResult getProductDetailList(String token, HashMap<String, Object> params) {
		Header header = new BasicHeader("CJ-Access-Token", token);
		String caString = HttpUtil.doGet("https://developers.cjdropshipping.com/api2.0/v1/product/query", params,
				header);
		JSONObject parseObject = JSONObject.parseObject(caString);

		return JSONObject.parseObject(parseObject.get("data").toString(), ProductDetailResult.class);
	}
}
